package com.xuecheng.media.controller;

import com.xuecheng.api.media.MediaApi;
import com.xuecheng.api.media.model.dto.MediaDTO;
import com.xuecheng.api.media.model.qo.QueryMediaModel;
import com.xuecheng.api.media.model.vo.MediaVo;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.media.convert.MediaConvert;
import com.xuecheng.media.service.MediaService;
import com.xuecheng.media.util.UAASecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class MediaController implements MediaApi {

    @Autowired
    private MediaService mediaService;


    @PostMapping("media/list")
    public PageVO<MediaDTO> queryMediaList(PageRequestParams params, @RequestBody QueryMediaModel model) {
        Long companyId = UAASecurityUtil.getCompanyId();
        PageVO<MediaDTO> pageVO = mediaService.queryMediaList(params, model, companyId);
        return pageVO;
    }

    @PostMapping("media")
    public MediaDTO createMedia(@RequestBody MediaVo vo) {
        Long companyId = UAASecurityUtil.getCompanyId();
        MediaDTO dto = MediaConvert.INSTANCE.vo2dto(vo);
        dto.setCompanyId(companyId);
        MediaDTO result = mediaService.createMedia(dto);
        return result;
    }

    @GetMapping("l/media/{mediaId}")
    public RestResponse<MediaDTO> getMediaById4Service(@PathVariable("mediaId") Long mediaId) {
        RestResponse<MediaDTO> response = mediaService.getMediaById4Service(mediaId);
        return response;
    }

    @DeleteMapping("media/{mediaId}")
    public void removeMediaById(@PathVariable("mediaId") Long mediaId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        mediaService.removeMediaById(mediaId, companyId);
    }

    @GetMapping("media/preview/{mediaId}")
    public String previewVideo(@PathVariable("mediaId") Long mediaId) {
        return mediaService.preViewMedia(mediaId);
    }

}
